package com.common.uilt;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

/**
 * 产生流水号工具类
 * 
 * @version V1.0
 * @date: 2015年5月17日22:31:59
 */
public class SerialNum {
	private String count = "00000";
	SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
	private String dateValue;
	private Map<String,String> mapkey = new HashMap<String, String>();

	public SerialNum() {
		this.dateValue = sdf.format(new Date());
		this.mapkey.put("CK", "出库单");
		this.mapkey.put("BF", "报废单");
		this.mapkey.put("DB", "调拨单");
		this.mapkey.put("TK", "退库单");
		this.mapkey.put("PD", "盘点单");
		this.mapkey.put("RK", "入库单");
		this.mapkey.put("SX", "送修单");
		this.mapkey.put("JC", "出借单");
		this.mapkey.put("GH", "出借单");
	}

	/**
	 * 产生流水号
	 */
	public synchronized String getMoveOrderNo(String type) {
		long No = 0;

		String nowdate = sdf.format(new Date());

		if (!nowdate.equals(dateValue)) {
			count = "00000";
			dateValue = String.valueOf(No);
		}
		// String num = String.valueOf(No);
		// nowdate += getNo(count);
		// nowdate = + nowdate;
		return type.toUpperCase() + nowdate + getNo(count);
	}

	/**
	 * 获取撤展单序列号
	 */
	public synchronized String getMoveOrderNo(String serialNum, String type) {
		if (serialNum != null && !"".equals(serialNum)) {
			String nyr = StringUtils.substring(serialNum, 2, 10); // 获取年月日字符串
			String countV = StringUtils.substring(serialNum, 10); // 获取流水号
			if (Integer.valueOf(countV) > Integer.valueOf(count)) {
				dateValue = nyr;
				count = String.valueOf(countV);
			}
		}
		return getMoveOrderNo(type);
	}

	/**
	 * 返回当天的订单数+1
	 */
	public String getNo(String s) {
		String rs = s;
		int i = Integer.parseInt(rs);
		i += 1;
		rs = "" + i;
		for (int j = rs.length(); j < 3; j++) {
			rs = "0" + rs;
		}
		count = rs;
		return rs;
	}

	public Map<String, String> getMapkey() {
		return mapkey;
	}

	public void setMapkey(Map<String, String> mapkey) {
		this.mapkey = mapkey;
	}

	public static void main(String[] args) {
		SerialNum s = new SerialNum();
		for (int i = 0; i < 10; i++) {
			System.out.println(s.getMoveOrderNo("ck"));
		}
	}
}